﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class ColorPicker : ICommandUIDefinition, IControlsChild, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Command { get; set; }
        public string CommandPreview { get; set; }
        public string CommandRevert { get; set; }
        public string Id { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string TemplateAlias { get; set; }
        public Colors InnerColors { get; set; }

        public ColorPicker()
        {}

        public ColorPicker(XmlNode node):this()
        {
            try
            {
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.CommandPreview = XmlHelper.GetAttribute(node, "CommandPreview");
                this.CommandRevert = XmlHelper.GetAttribute(node, "CommandRevert");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");

                if (node.SelectSingleNode("Colors") != null)
                {
                    this.InnerColors = new Colors(node.SelectSingleNode("Colors"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode customActionNode)
        {
            try
            {
                XmlNode colorPickerNode = XmlHelper.AddNode(customActionNode, "ColorPicker");

                XmlHelper.AddAttribute(colorPickerNode, "Command", this.Command);
                XmlHelper.AddAttribute(colorPickerNode, "CommandPreview", this.CommandPreview);
                XmlHelper.AddAttribute(colorPickerNode, "CommandRevert", this.CommandRevert);
                XmlHelper.AddAttribute(colorPickerNode, "Id", this.Id);
                XmlHelper.AddAttribute(colorPickerNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(colorPickerNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(colorPickerNode, "TemplateAlias", this.TemplateAlias);

                if (this.InnerColors != null)
                {
                    this.InnerColors.AddNode(colorPickerNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("ColorPicker");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Colors" }, true);

                if (this.InnerColors != null)
                {
                    this.InnerColors.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Colors))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    this.InnerColors = (Colors)child;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Colors)
                {
                    this.InnerColors = null;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
